﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using CommonLib;
using System;
using UnityEditorInternal;
using System.Linq;

public class WindowEditorRole : EditorWindow
{
	[MenuItem("Editor/Role/Eidt")]
	static void OpenThis()
	{
		WindowEditorRole window = EditorWindow.GetWindowWithRect<WindowEditorRole>(new Rect(200,200,600,800),true ,"角色控制信息设置",false);
		window.Show();
	}
	[MenuItem("Editor/Role/Create")]
	static void CreateARole()
	{
		ScriptObjectRole scriptObjectRole = ScriptableObject.CreateInstance<ScriptObjectRole>();
		//初始设置Role信息
		scriptObjectRole.RoleModelName = "Temp";
		scriptObjectRole.StageInfor = new List<ScriptObjectRole.RoleStage>()
		{
			new ScriptObjectRole.RoleStage(){
				 StageIndex = 0,
				 StageTexture = null,
				 StageSize = new Vector3(1,1,1),
				 StageMinScore = 0,
				 StageSpeed = 4.0f,

				 ColliderType = ScriptObjectRole.ColliderType.Circle,
				 ColliderCenter = new Vector2(0,0), ColliderRadius = 1.0f,
			},
		};
		AssetDatabase.CreateAsset(scriptObjectRole, Path.Combine(EditorUtil.GetSelectFolder(), "TempRole.asset"));
		AssetDatabase.SaveAssets();
	}

	ScriptObjectRole ObjectRole = null;
	SerializedObject SerializedObject = null;
	Vector2 ScrollPosition = Vector2.zero;

	List<bool> ToogleList = new List<bool>();

	ReorderableList reorderableList = null;

	//标记List的height

	bool IsEnable = false;
	private void OnEnable()
	{
		if (null == ObjectRole)
		{
			if (!EditorUtil.TryGetSelectT<ScriptObjectRole>(out ObjectRole))
			{
				return;
			}
		}
		SerializedObject = new SerializedObject(ObjectRole);
		SerializedProperty StageInforProperty = SerializedObject.FindProperty("StageInfor");
		List<float> _ReOrderHeights = new List<float>(StageInforProperty.arraySize);
		//初次创建时 设置回调函数 
		if (null == reorderableList)
		{
			reorderableList = new ReorderableList(SerializedObject, StageInforProperty, true, true, true, true);	
			reorderableList.drawElementCallback = (rect, index, isActive, isFocused) =>
			{
				float curHeight = 0;
				SerializedProperty indexProperty = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
				SerializedProperty StageIndexProperty = indexProperty.FindPropertyRelative("StageIndex");
				SerializedProperty StageTextureProperty = indexProperty.FindPropertyRelative("StageTexture");
				SerializedProperty StageSizeProperty = indexProperty.FindPropertyRelative("StageSize");
				SerializedProperty StageMinScoreProperty = indexProperty.FindPropertyRelative("StageMinScore");
				SerializedProperty StageSpeedProperty = indexProperty.FindPropertyRelative("StageSpeed");
				SerializedProperty ColliderTypeProperty = indexProperty.FindPropertyRelative("ColliderType");

				SerializedProperty ColliderCenterProperty = indexProperty.FindPropertyRelative("ColliderCenter");
				SerializedProperty ColliderSizeProperty = indexProperty.FindPropertyRelative("ColliderSize");
				SerializedProperty ColliderRadiusProperty = indexProperty.FindPropertyRelative("ColliderRadius");
				SerializedProperty ColliderPointProperty = indexProperty.FindPropertyRelative("ColliderPoint");

				StageIndexProperty.intValue = EditorGUI.IntField(new Rect(rect.x + 10, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 20, EditorGUIUtility.singleLineHeight),"StageIndex" ,StageIndexProperty.intValue);
				StageMinScoreProperty.intValue = EditorGUI.IntField(new Rect(rect.x + 10, rect.y + 2 * EditorGUIUtility.singleLineHeight, rect.width - 20, EditorGUIUtility.singleLineHeight),"StageScore" ,StageMinScoreProperty.intValue);
				StageSpeedProperty.floatValue = EditorGUI.FloatField(new Rect(rect.x + 10, rect.y + 3 * EditorGUIUtility.singleLineHeight, rect.width - 20, EditorGUIUtility.singleLineHeight),"StageSpeed",StageSpeedProperty.floatValue);
				StageSizeProperty.vector3Value = EditorGUI.Vector3Field(new Rect(rect.x + 10, rect.y + 4* EditorGUIUtility.singleLineHeight, rect.width - 20, EditorGUIUtility.singleLineHeight),"StageSize" , StageSizeProperty.vector3Value);
				StageTextureProperty.objectReferenceValue = EditorGUI.ObjectField(new Rect(rect.x + 10, rect.y + 7 * EditorGUIUtility.singleLineHeight, 200, 200),"StageImage", StageTextureProperty.objectReferenceValue, typeof(Texture2D),false);
				ColliderTypeProperty.enumValueIndex = (int)(ScriptObjectRole.ColliderType)EditorGUI.EnumPopup(new Rect(rect.x + 10, rect.y + 8*EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight)
					,"碰撞框类型",(ScriptObjectRole.ColliderType)ColliderTypeProperty.enumValueIndex);
				float itemHeight = 0;
				switch ((ScriptObjectRole.ColliderType)ColliderTypeProperty.enumValueIndex)
				{
					case ScriptObjectRole.ColliderType.Box:
					case ScriptObjectRole.ColliderType.Capsule:
						itemHeight = 4 * EditorGUIUtility.singleLineHeight;
						ColliderCenterProperty.vector2Value = EditorGUI.Vector2Field(new Rect(rect.x + 10, rect.y + 9 * EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), "ColliderCenter", ColliderCenterProperty.vector2Value);
						ColliderSizeProperty.vector2Value = EditorGUI.Vector2Field(new Rect(rect.x + 10, rect.y + 11 * EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), "ColliderSize", ColliderSizeProperty.vector2Value);

						break;
					case ScriptObjectRole.ColliderType.Circle:
						itemHeight = 3 * EditorGUIUtility.singleLineHeight;
						ColliderCenterProperty.vector2Value = EditorGUI.Vector2Field(new Rect(rect.x + 10, rect.y + 9 * EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), "ColliderCenter", ColliderCenterProperty.vector2Value);
						ColliderRadiusProperty.floatValue = EditorGUI.FloatField(new Rect(rect.x + 10, rect.y + 11 * EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), "ColliderRadius", ColliderRadiusProperty.floatValue);

						break;
					case ScriptObjectRole.ColliderType.Polygon:
						//ShowButtons();
						GUILayout.BeginHorizontal();
						
						//GUI.Button(new Rect(rect.x + 10, rect.y + 9 * EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), );
						if(GUI.Button(new Rect(rect.x + 20, rect.y + 9 * EditorGUIUtility.singleLineHeight + 60, 40, EditorGUIUtility.singleLineHeight), duplicateButtonContent))
						{
							ColliderPointProperty.InsertArrayElementAtIndex(ColliderPointProperty.arraySize);
						}
						if(GUI.Button(new Rect(rect.x + 80, rect.y + 9 * EditorGUIUtility.singleLineHeight + 60, 40, EditorGUIUtility.singleLineHeight), deleteButtonContent)){
							if(ColliderPointProperty.arraySize - 1 >=0)
								ColliderPointProperty.DeleteArrayElementAtIndex(ColliderPointProperty.arraySize - 1);
						}
						GUILayout.EndHorizontal();
						for (int pi = 0; pi < ColliderPointProperty.arraySize; pi++)
						{
							SerializedProperty point = ColliderPointProperty.GetArrayElementAtIndex(pi);
							point.vector3Value = EditorGUI.Vector3Field(new Rect(rect.x + 10, rect.y + (10 + pi * 2)* EditorGUIUtility.singleLineHeight + 60, rect.width - 20, EditorGUIUtility.singleLineHeight), $"point {pi} :", point.vector3Value);
						}
						itemHeight = 2 * ColliderPointProperty.arraySize * EditorGUIUtility.singleLineHeight;
						break;
				}

				curHeight = 10 * EditorGUIUtility.singleLineHeight + 80 + itemHeight;
				rect.height = _ReOrderHeights[index] = curHeight;
			};
			reorderableList.elementHeightCallback = (index) =>
			{
				Repaint();
				float height = 0;
				try
				{
					height = _ReOrderHeights[index];
				}
				catch (ArgumentOutOfRangeException e)
				{
					Debug.Log(e.StackTrace);
					//Debug.LogError($"This index  {index} is Out of Bounce");
					height = 0;
				}
				finally
				{
					float[] temp = _ReOrderHeights.ToArray();
					Array.Resize(ref temp, StageInforProperty.arraySize);
					_ReOrderHeights = temp.ToList();
				}
				return height;
			};
		}
		IsEnable = true;
	}

	private void OnGUI()
	{
		if (IsEnable == false)
			return;
		//刷新Object
		SerializedObject.Update();

		ScrollPosition = EditorGUILayout.BeginScrollView(ScrollPosition, GUILayout.Width(600), GUILayout.Height(800));
		SerializedProperty RoleModelNameProperty = SerializedObject.FindProperty("RoleModelName");
		SerializedProperty DefaultStageProperty = SerializedObject.FindProperty("DefaultStage");
		SerializedProperty StageInforProperty = SerializedObject.FindProperty("StageInfor");
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.ObjectField("编辑角色", ObjectRole, typeof(ScriptObjectRole), false);
		EditorGUILayout.EndHorizontal();
		EditorGUILayout.Space();
		RoleModelNameProperty.stringValue = EditorGUILayout.TextField("角色模板名称", RoleModelNameProperty.stringValue);
		DefaultStageProperty.intValue = EditorGUILayout.IntField("初始阶段", DefaultStageProperty.intValue);
		EditorGUILayout.Space();

		#region 
		/*
		for (int index = 0; index < StageInforProperty.arraySize; index++)
		{

			EditorGUILayout.BeginHorizontal("Button");
			ShowButtons(index);
			EditorGUILayout.EndHorizontal();
			if (ToogleList[index])
			{
				SerializedProperty indexProperty = StageInforProperty.GetArrayElementAtIndex(index);
				SerializedProperty StageIndexProperty = indexProperty.FindPropertyRelative("StageIndex");
				SerializedProperty StageTextureProperty = indexProperty.FindPropertyRelative("StageTexture");
				SerializedProperty StageSizeProperty = indexProperty.FindPropertyRelative("StageSize");
				SerializedProperty StageMinScoreProperty = indexProperty.FindPropertyRelative("StageMinScore");
				SerializedProperty StageSpeedProperty = indexProperty.FindPropertyRelative("StageSpeed");
				SerializedProperty ColliderTypeProperty = indexProperty.FindPropertyRelative("ColliderType");

				SerializedProperty ColliderCenterProperty = indexProperty.FindPropertyRelative("ColliderCenter");
				SerializedProperty ColliderSizeProperty = indexProperty.FindPropertyRelative("ColliderSize");
				SerializedProperty ColliderRadiusProperty = indexProperty.FindPropertyRelative("ColliderRadius");
				SerializedProperty ColliderPointProperty = indexProperty.FindPropertyRelative("ColliderPoint");
				EditorGUILayout.BeginVertical(GUI.skin.box);
				//GUILayout.BeginVertical(GUILayout.Width(500),GUILayout.ExpandWidth(true), GUILayout.Height(300), GUILayout.ExpandHeight(true));

				StageIndexProperty.intValue = EditorGUILayout.IntField("阶段次序", StageIndexProperty.intValue);
				StageMinScoreProperty.intValue = EditorGUILayout.IntField("阶段分数", StageMinScoreProperty.intValue);
				StageSpeedProperty.floatValue = EditorGUILayout.FloatField("阶段速度", StageSpeedProperty.floatValue);
				StageTextureProperty.objectReferenceValue = EditorGUILayout.ObjectField("阶段图片", StageTextureProperty.objectReferenceValue, typeof(Texture2D), false);
				ScriptObjectRole.ColliderType colliderType = (ScriptObjectRole.ColliderType)ColliderTypeProperty.enumValueIndex;
				colliderType = (ScriptObjectRole.ColliderType)EditorGUILayout.EnumPopup(colliderType);
				ColliderTypeProperty.enumValueIndex = (int)colliderType;


				switch (colliderType)
				{
					case ScriptObjectRole.ColliderType.Box:
					case ScriptObjectRole.ColliderType.Capsule:
						ColliderCenterProperty.vector2Value = EditorGUILayout.Vector2Field("碰撞中心", ColliderCenterProperty.vector2Value);
						ColliderSizeProperty.vector2Value = EditorGUILayout.Vector2Field("碰撞范围", ColliderCenterProperty.vector2Value);
						break;
					case ScriptObjectRole.ColliderType.Circle:
						ColliderCenterProperty.vector2Value = EditorGUILayout.Vector2Field("碰撞中心", ColliderCenterProperty.vector2Value);
						ColliderRadiusProperty.floatValue = EditorGUILayout.FloatField("碰撞半径", ColliderRadiusProperty.floatValue);
						break;
					case ScriptObjectRole.ColliderType.Polygon:
						for (int pi = 0; pi < ColliderPointProperty.arraySize; pi++)
						{
							SerializedProperty pointIndex = ColliderPointProperty.GetArrayElementAtIndex(pi);
							pointIndex.vector3Value = EditorGUILayout.Vector3Field($"{pi}碰撞点", pointIndex.vector3Value);
						}
						break;
				}
				EditorGUILayout.EndVertical();
				EditorGUILayout.Space();
				//GUILayout.BeginVertical();
			}
		}
		*/
		#endregion

		reorderableList.DoLayoutList();
		EditorGUILayout.EndScrollView();
		//应用Object修改
		SerializedObject.ApplyModifiedProperties();
	}

	private static GUIContent
		moveButtonContent = new GUIContent("\u21b4", "move down"),
		duplicateButtonContent = new GUIContent("+", "duplicate"),
		deleteButtonContent = new GUIContent("-", "delete");
	private void ShowButtons()
	{
		if (GUILayout.Button(duplicateButtonContent))
		{

		}
		if (GUILayout.Button(deleteButtonContent))
		{

		}
	}
}
